﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:MvvmViewModelLocator1 xmlns:vm="clr-namespace:uTorrentSL"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/

using Cleverscape.UTorrentClient.WebClient;
using uTorrentSL.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.IO.IsolatedStorage;
using GalaSoft.MvvmLight.Command;
using System.Windows.Controls;

namespace uTorrentSL.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the MvvmViewModelLocator1 in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:MvvmViewModelLocator1 xmlns:vm="clr-namespace:uTorrentSL"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class ViewModelLocator
    {
         #region DATA SOURCE
        private static IUTorrentData dataService;

        public static IUTorrentData DataService
        {
            get 
            {
                if (dataService == null)
                    dataService = GetDataService();
                return dataService; 
            }
            set { dataService = value; }
        }

        public static IUTorrentData GetDataService()
        {
            if (ViewModelBase.IsInDesignModeStatic)
            {
                return new UTorrentDesignData();
            }
           else
            {
                UTorrentWebClient d = null;
                if (!string.IsNullOrEmpty(HostPort)
                    && !string.IsNullOrEmpty(Username)
                    && !string.IsNullOrEmpty(Password))
                {
                    d = new UTorrentWebClient(HostPort, Username, Password, true, 5);
                    Messenger.Default.Send<string>("", "LoadStarted");
                    d.InitialLoadComplete += new System.EventHandler(d_InitialLoadComplete);
                }
                    
                
                return d;
            }

        }

        private static IsolatedStorageSettings iss;

        public static IsolatedStorageSettings Settings
        {
            get 
            {
                if (iss == null)
                {
                    iss = IsolatedStorageSettings.ApplicationSettings;
                    if (!iss.Contains("host:port"))
                        iss.Add("host:port", "");
                    if (!iss.Contains("username"))
                        iss.Add("username", "");
                    if (!iss.Contains("password"))
                        iss.Add("password", "");
                }
                return iss; 
            }
            set { iss = value; }
        }

        public static string HostPort
        {
            get
            {
                return Settings["host:port"].ToString();
            }
            set
            {
                Settings["host:port"] = value;
            }
        }
        public static string Username
        {
            get
            {
                return Settings["username"].ToString();
            }
            set
            {
                Settings["username"] = value;
            }
        }
        public static string Password
        {
            get
            {
                return Settings["password"].ToString();
            }
            set
            {
                Settings["password"] = value;
            }
        }

        static void d_InitialLoadComplete(object sender, System.EventArgs e)
        {
            Messenger.Default.Send<string>("", "LoadComplete");
            Messenger.Default.Send<string>("TorrentsView", "LoadView");
            
            
        }

        #endregion;


        

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view models
            ////}
            ////else
            ////{
            ////    // Create run time view models
            ////}
            LaunchModalCommand = new RelayCommand<string>(LaunchModal);

            CloseModalCommand = new RelayCommand(CloseModal);

            CreateMain();
            CreateHomeViewModel();
        }

        public static Frame MainFrame { get; set; }

        public static void Navigate(string v)
        {
            MainFrame.Navigate(ViewRetrieve.ViewUri(v));
        }

        public static void GoBack()
        {
            MainFrame.GoBack();
        }

        public static void CloseModal()
        {
            ModalWindow.Close();
        }

        public static void LaunchModal(string v)
        {
            ModalWindow.Content = ViewRetrieve.Retrieve(v);
            ModalWindow.Show();
        }

        private static ChildWindow _modalWindow;

        public static ChildWindow ModalWindow
        {
            get 
            {
                if (_modalWindow == null)
                    _modalWindow = new ChildWindow();
                return _modalWindow; 
            }
            set { _modalWindow = value; }
        }
        

        #region FeedsViewModel
        private static FeedsViewModel _feedsViewModel;

        /// <summary>
        /// Gets the FeedsViewModel property.
        /// </summary>
        public static FeedsViewModel FeedsViewModelStatic
        {
            get
            {
                if (_feedsViewModel == null)
                {
                    CreateFeedsViewModel();
                }

                return _feedsViewModel;
            }
        }

        /// <summary>
        /// Gets the FeedsViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public FeedsViewModel FeedsViewModel
        {
            get
            {
                return FeedsViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the FeedsViewModel property.
        /// </summary>
        public static void ClearFeedsViewModel()
        {
            if (_feedsViewModel != null)
            {
                _feedsViewModel.Cleanup();
                _feedsViewModel = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the FeedsViewModel property.
        /// </summary>
        public static void CreateFeedsViewModel()
        {
            if (_feedsViewModel == null)
            {
                _feedsViewModel = new FeedsViewModel();
                CreateFeedViewModel();
            }
        }
        #endregion

        #region MainViewModel
        private static MainViewModel _main;

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            _main.Cleanup();
            _main = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel();
                CreateSettingsViewModel();
            }
        }

        #endregion;

        #region FiltersViewModel
        private static FiltersViewModel _filtersViewModel;

        /// <summary>
        /// Gets the FiltersViewModel property.
        /// </summary>
        public static FiltersViewModel FiltersViewModelStatic
        {
            get
            {
                if (_filtersViewModel == null)
                {
                    CreateFiltersViewModel();
                }

                return _filtersViewModel;
            }
        }

        /// <summary>
        /// Gets the FiltersViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public FiltersViewModel FiltersViewModel
        {
            get
            {
                return FiltersViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the FiltersViewModel property.
        /// </summary>
        public static void ClearFiltersViewModel()
        {
            _filtersViewModel.Cleanup();
            _filtersViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the FiltersViewModel property.
        /// </summary>
        public static void CreateFiltersViewModel()
        {
            if (_filtersViewModel == null)
            {
                _filtersViewModel = new FiltersViewModel();
                CreateFilterViewModel();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        
        #endregion

        #region SettingsViewModel
        private static SettingsViewModel _settingsViewModel;

        /// <summary>
        /// Gets the SettingsViewModel property.
        /// </summary>
        public static SettingsViewModel SettingsViewModelStatic
        {
            get
            {
                if (_settingsViewModel == null)
                {
                    CreateSettingsViewModel();
                }

                return _settingsViewModel;
            }
        }

        /// <summary>
        /// Gets the SettingsViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public SettingsViewModel SettingsViewModel
        {
            get
            {
                return SettingsViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the SettingsViewModel property.
        /// </summary>
        public static void ClearSettingsViewModel()
        {
            _settingsViewModel.Cleanup();
            _settingsViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the SettingsViewModel property.
        /// </summary>
        public static void CreateSettingsViewModel()
        {
            if (_settingsViewModel == null)
            {
                _settingsViewModel = new SettingsViewModel();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        #endregion

        #region TorrentsViewModel

        private static TorrentsViewModel _torrentsViewModel;

        /// <summary>
        /// Gets the TorrentsViewModel property.
        /// </summary>
        public static TorrentsViewModel TorrentsViewModelStatic
        {
            get
            {
                if (_torrentsViewModel == null)
                {
                    CreateTorrentsViewModel();
                }

                return _torrentsViewModel;
            }
        }

        /// <summary>
        /// Gets the TorrentsViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public TorrentsViewModel TorrentsViewModel
        {
            get
            {
                return TorrentsViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the TorrentsViewModel property.
        /// </summary>
        public static void ClearTorrentsViewModel()
        {
            _torrentsViewModel.Cleanup();
            _torrentsViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the TorrentsViewModel property.
        /// </summary>
        public static void CreateTorrentsViewModel()
        {
            if (_torrentsViewModel == null)
            {
                _torrentsViewModel = new TorrentsViewModel();
                CreateTorrentViewModel();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        

        #endregion

        #region TorrentViewModel

        private static TorrentViewModel _torrentViewModel;

        /// <summary>
        /// Gets the TorrentViewModel property.
        /// </summary>
        public static TorrentViewModel TorrentViewModelStatic
        {
            get
            {
                if (_torrentViewModel == null)
                {
                    CreateTorrentViewModel();
                }

                return _torrentViewModel;
            }
        }

        /// <summary>
        /// Gets the TorrentViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public TorrentViewModel TorrentViewModel
        {
            get
            {
                return TorrentViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the TorrentViewModel property.
        /// </summary>
        public static void ClearTorrentViewModel()
        {
            _torrentViewModel.Cleanup();
            _torrentViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the TorrentViewModel property.
        /// </summary>
        public static void CreateTorrentViewModel()
        {
            if (_torrentViewModel == null)
            {
                _torrentViewModel = new TorrentViewModel();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>


        private static FilterViewModel _filterViewModel;

        /// <summary>
        /// Gets the FilterViewModel property.
        /// </summary>
        public static FilterViewModel FilterViewModelStatic
        {
            get
            {
                if (_filterViewModel == null)
                {
                    CreateFilterViewModel();
                }

                return _filterViewModel;
            }
        }

        /// <summary>
        /// Gets the FilterViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public FilterViewModel FilterViewModel
        {
            get
            {
                return FilterViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the FilterViewModel property.
        /// </summary>
        public static void ClearFilterViewModel()
        {
            _filterViewModel.Cleanup();
            _filterViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the FilterViewModel property.
        /// </summary>
        public static void CreateFilterViewModel()
        {
            if (_filterViewModel == null)
            {
                _filterViewModel = new FilterViewModel();
            }
        }

        private static FeedViewModel _feedViewModel;

        /// <summary>
        /// Gets the FeedViewModel property.
        /// </summary>
        public static FeedViewModel FeedViewModelStatic
        {
            get
            {
                if (_feedViewModel == null)
                {
                    CreateFeedViewModel();
                }

                return _feedViewModel;
            }
        }

        /// <summary>
        /// Gets the FeedViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public FeedViewModel FeedViewModel
        {
            get
            {
                return FeedViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the FeedViewModel property.
        /// </summary>
        public static void ClearFeedViewModel()
        {
            _feedViewModel.Cleanup();
            _feedViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the FeedViewModel property.
        /// </summary>
        public static void CreateFeedViewModel()
        {
            if (_feedViewModel == null)
            {
                _feedViewModel = new FeedViewModel();
            }
        }


        private static HomeViewModel _homeViewModel;

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static HomeViewModel HomeViewModelStatic
        {
            get
            {
                if (_homeViewModel == null)
                {
                    CreateHomeViewModel();
                }

                return _homeViewModel;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public HomeViewModel HomeViewModel
        {
            get
            {
                return HomeViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearHomeViewModel()
        {
            _homeViewModel.Cleanup();
            _homeViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateHomeViewModel()
        {
            if (_homeViewModel == null)
            {
                _homeViewModel = new HomeViewModel();
            }
        }

        

        #endregion



        public RelayCommand<string> LaunchModalCommand { get; set; }

        public RelayCommand CloseModalCommand { get; set; }
        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();
            ClearFeedsViewModel();
        }


    }
}